<?php

namespace App\Admin\Renderable;

use App\Models\AdminUser;
use App\Models\MessageRecord;
use App\Models\OrderServiceProfile;
use App\Models\OrderServiceSubmitDetail;
use App\Models\User;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;

class MessageRecordTable extends LazyRenderable
{
    public function render()
    {
        // 获取ID
        $id = $this->key;

        $data = MessageRecord::where('order_id', $id)
            ->where('message_type', $this->payload['message_type'])
            ->get(['order_id', 'user_id', 'message_id', 'message_type', 'admin_id', 'created_at'])
            ->toArray();
        foreach ($data as $key => &$val) {
            $val['user_id'] = User::find($val['user_id'])->username ?? '';
            if ($this->payload['message_type'] == 1) {
                $val['message_id'] = OrderServiceSubmitDetail::find($val['message_id'])->contents ?? '';
            } elseif ($this->payload['message_type'] == 2) {
                $val['message_id'] = OrderServiceProfile::find($val['message_id'])->contents ?? '';
            }

            $val['message_type'] = $this->payload['message_type'] == 1 ? '投稿详情（投稿进度）' : '补充资料';
            $val['admin_id'] = AdminUser::find($val['admin_id'])->username ?? '';
        }
        $titles = [
            '订单ID',
            '用户',
            '消息',
            '发送类型',
            '发送人',
            '发送时间',
        ];

        return Table::make($titles, $data);
    }
}
